package ar.fiuba.logger.filter;

import ar.fiuba.logger.LogFilter;
import ar.fiuba.logger.LogMessageInterface;

/**
 * The Class MessageMatchesRegularExpresionFilter.
 * 
 * This class implements a filter that won't allow messages that doesn't match the given regular
 * expression.
 */
public class MessageMatchesRegularExpressionFilter implements LogFilter {

  /** The regex. */
  private String regex;

  /**
   * Instantiates a new message matches regular expression filter.
   * 
   * @param regex the regex
   */
  public MessageMatchesRegularExpressionFilter(String regex) {
    this.regex = regex;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.filter.LogFilter#allow(ar.fiuba.logger.LogMessageInterface)
   */
  @Override
  public Boolean allow(LogMessageInterface logMessage) {
    return logMessage.getLogMessage().matches(regex);
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    MessageMatchesRegularExpressionFilter other = (MessageMatchesRegularExpressionFilter) obj;
    if (regex == null) {
      if (other.regex != null)
        return false;
    } else if (!regex.equals(other.regex))
      return false;
    return true;
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((regex == null) ? 0 : regex.hashCode());
    return result;
  }

}
